/******begin your code here******/
#include "LinkedList.h"
#include "LinkedListOp.h"
#include <iostream>
using std::cout;
using std::endl;

//默认构造函数
LinkedList::LinkedList() {size=0;head=new Node();}
//拷贝构造函数，构造一个逻辑上与参数内容相同的链表
LinkedList::LinkedList(const LinkedList&rhs) {
    size=rhs.getSize();
    head=new Node();
    Node* n1=head;
    Node* n2=rhs.head;
    for (int i=0;i<size;++i) {
        Node* newNode=new Node(n2->next->data);
        n1->next=newNode;
        n1=n1->next;
        n2=n2->next;
    }
}
//原生数组构造函数，构造一个内容与给定数组相同的链表
LinkedList::LinkedList(int const a[],int n) {
    size=n;
    head=new Node();
    Node* temp=head;
    for (int i=0;i<n;++i) {
        Node* newNode=new Node(a[i]);
        temp->next=newNode;
        temp=temp->next;
    }
}
//填充构造函数，构造一个内容为n个value的链表
LinkedList::LinkedList(int n,int value) {
    size=n;
    head=new Node();
    Node *temp=head;
    for (int i=0;i<n;++i) {
        Node* newNode=new Node(value);
        temp->next=newNode;
        temp=temp->next;
    }
}
//析构函数，一定要自行实现，否则有内存泄漏
LinkedList::~LinkedList() {
    while (head->next) {
        Node *temp=head->next;
        head->next=temp->next;
        --size;
        delete temp;
    }
}
//insert函数在pos位置（从0开始编号）插入一个值为value的元素
void LinkedList::insert(int pos,int value) {
    Node* newNode=new Node();
    Node* temp=advance(pos);
    newNode->data=value;
    newNode->next=temp->next;
    temp->next=newNode;
    ++size;
}
//remove函数将pos位置上的元素删除
void LinkedList::remove(int pos) {
    Node* temp=advance(pos);
    Node* delNode=temp->next;
    temp->next=delNode->next;
    delete delNode;
    --size;
}
//at函数返回pos位置上元素的值
int LinkedList::at(int pos)const {
    return advance(pos)->next->data;
}
//modify函数将pos位置上的元素值修改为newValue
void LinkedList::modify(int pos,int newValue) {
    advance(pos)->next->data=newValue;
}
//disp函数将所保存的数据输出到屏幕，输出为一行，每个数据后面接一个空格
void LinkedList::disp()const {
    Node *temp=head;
    while (temp->next) {
        cout<<temp->next->data<<" ";
        temp=temp->next;
    }
    cout<<endl;
}
//返回指定位置的指针
LinkedList::Node* LinkedList::advance(int pos)const {
    Node* temp=head;
    for (int i=0;i<pos;++i) {
        temp=temp->next;
    }
    return temp;
}
//赋值运算符重载
LinkedList& LinkedList::operator=(const LinkedList&rhs) {
    while (head->next) {
        Node *temp=head->next;
        head->next=temp->next;
        --size;
        delete temp;
    }
    size=rhs.getSize();
    head=new Node();
    Node* n1=head;
    Node* n2=rhs.head;
    for (int i=0;i<size;++i) {
        Node* newNode=new Node(n2->next->data);
        n1->next=newNode;
        n1=n1->next;
        n2=n2->next;
    }
    return *this;
}
LinkedList& LinkedList::operator+=(const LinkedList&rhs) {
    LinkedList::Node* temp=advance(size);
    LinkedList::Node* n2=rhs.head;
    while (n2->next) {
        LinkedList::Node* newNode=new LinkedList::Node();
        newNode->data=n2->next->data;
        temp->next=newNode;
        n2=n2->next;
        temp=temp->next;
        ++size;
    }
    return *this;
}
//方括号运算符重载
int& LinkedList::operator [] (int pos) {
    return advance(pos)->next->data;
}
const int& LinkedList::operator [] (int pos)const {
    return advance(pos)->next->data;
}
//该函数比较2个链表，如果∗this<rhs则返回负数，相等则返回0，再则返回正数。然后利用该函数实现关系运算符重载
int LinkedList::compareTo(const LinkedList&rhs)const {
    LinkedList::Node* n1=head;
    LinkedList::Node* n2=rhs.head;
    while (n2->next) {
        if (n1->next==nullptr) {
            return -1;
        } else {
            int x=n1->next->data;
            int y=n2->next->data;
            if (x<y) {
                return -1;
            } else if (x>y) {
                return 1;
            } else {
                n1=n1->next;
                n2=n2->next;
            }
        }
    }
    return size>rhs.size;
}
//流输出
void LinkedList::disp(ostream&os)const {
    LinkedList::Node* temp=head;
    while (temp->next) {
        os<<(temp->next->data)<<" ";
        temp=temp->next;
    }
}

/******end your code******/